package com.sub.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sub.pojo.User;
import com.sub.service.IUserService;
import com.sub.utils.BaseResult;
import com.sub.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Submerge.
 * @since 2022-11-03
 */
@RestController
@RequestMapping("/sub/user")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 用户列表
     * @return
     */
    @GetMapping("/list")
    public R userList(){
        //List<User> records = userService.list();
        IPage<User> page = new Page<>(1,10);
        IPage<User> iPage = userService.page(page);
        return R.success(iPage);
    }


    /**
     * 新增用户
     * @param user
     * @return
     */
    @PostMapping("/addUser")
    public ResponseEntity<String> addUser(@RequestBody User user){
        userService.save(user);
        return ResponseEntity.ok("新增成功！");
    }

    /**
     * 用户详情
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public ResponseEntity<User> userDetail(@PathVariable String id){
        User byId = userService.getById(id);
        return ResponseEntity.ok(byId);
    }





}

